package com.rjwh.dingdong.client.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.TagConfig;
import com.rjwh.dingdong.client.bean.localbean.TopicData;
import com.rjwh.dingdong.client.util.PicPathUtil;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.rjwh.dingdong.client.view.NameTagView;
import com.rjwh.dingdong.client.widget.MyGridView;
import com.rjwh.dingdong.client.widget.RoundImageView;
import com.wby.base.AdapterBase;

import java.util.List;

/**
 * @author Kay
 * @ClassName CommunityPostMyMainPageAdapter
 * @Description
 * @date 2016-7-29
 * @history 2016-7-29 author: description:
 */
public class CommunityPostMyMainPageAdapter extends AdapterBase<TopicData> {

    private Context context;
    private int screen;

    public CommunityPostMyMainPageAdapter(Context context) {
        this.context = context;
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width_screen = dm.widthPixels;
        screen = (int) Math.round(width_screen * 0.75);
    }

    @Override
    protected View getExView(int i, View view, ViewGroup viewGroup) throws Exception {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.community_post_item, null);
        }
        RoundImageView imgView = ViewHolder.get(view, R.id.item_post_head_img_view);
        TextView usernameTV = ViewHolder.get(view, R.id.item_post_user_name);
        LinearLayout usernametagRoot = ViewHolder.get(view, R.id.item_post_user_name_tag_root_ll);
        TextView timeTV = ViewHolder.get(view, R.id.item_post_send_time);
        TextView positionTV = ViewHolder.get(view, R.id.item_post_position_tv);
        TextView contentTV = ViewHolder.get(view, R.id.item_post_content);
        MyGridView picGV = ViewHolder.get(view, R.id.item_post_pic_gridview);

        TopicData topicData = mList.get(i);
        List<TagConfig> config = topicData.getTagconfig();
        // 设置nameTag
        usernametagRoot.removeAllViews();
        if (config != null) {
            for (int j = 0; j < config.size(); j++) {
                NameTagView nameTagView = new NameTagView(context);
                TagConfig topicConfig = config.get(j);
                nameTagView.setTag(topicConfig, R.drawable.topic_username_tag_shape, false);
                nameTagView.setPadding(4, 1, 4, 1);
                usernametagRoot.addView(nameTagView);
            }
        }
        if (!AsStrUtils.isEmpty(topicData.getName())) {
            usernameTV.setText(topicData.getName());
        }
        if (topicData.getAvatar() != null) {
            Glide.with(context).load(PicPathUtil.getPicUrl(topicData.getAvatar())).placeholder(R.drawable.icon_defult_avatar).error(R.drawable.icon_defult_avatar).dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).into(imgView);
        }
        if (!AsStrUtils.isEmpty(topicData.getTime())) {
            if (AsStrUtils.isEmpty(topicData.getRead()) || topicData.getRead().equals("0")) {
                timeTV.setText(topicData.getTime());
            } else {
                timeTV.setText(topicData.getTime() + "/浏览" + topicData.getRead());
            }
        }
        if (!AsStrUtils.isEmpty(topicData.getRegion())) {
            positionTV.setText(topicData.getRegion());
        } else {
            positionTV.setText("未知");
        }

        if (!AsStrUtils.isEmpty(topicData.getTitle())) {
            contentTV.setText(topicData.getTitle());
        }
        MyPicAdapter adapter = new MyPicAdapter(context, topicData.getImage(), null, screen);
        // 设置GridView不抢夺焦点
        picGV.setClickable(false);
        picGV.setPressed(false);
        picGV.setEnabled(false);
        picGV.setAdapter(adapter);

        return view;
    }

    @Override
    protected void onReachBottom() {

    }
}
